package com.tsit.study.netty.server.controller;

import com.tsit.study.netty.server.handler.NettyServerHandler;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangxuzhi
 * @date 2020/11/28 8:52
 */
@RestController
@RequestMapping("/send")
public class SendMsgController {

    @GetMapping("/msg/{msg}")
    public void sendMsg(@PathVariable("msg") String msg) {
        FullHttpResponse response =
                new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(msg.toString().getBytes()));
        NettyServerHandler.channelMap.forEach((k,v) -> {
            v.writeAndFlush(response);
        });
    }
}
